package com.xframe.xdal.core.model.sqlbuilder.proc;

import com.xframe.xdal.core.model.sqlbuilder.BaseSqlBuilder;
import com.xframe.xdal.core.model.sqlbuilder.BuilderContext;
import com.xframe.xdal.core.model.sqlbuilder.SqlExpression;

/**
 * 功能描述: 分页总数量查询sqlBuiler
 *
 * @author xufeng
 * @version 1.0
 * @date 2022-09-26
 */
public class CountPageSqlBuilder extends BaseSqlBuilder {
    /**
     * count sql 语句
     */
    public static final String COUNT_SQL = "select count(1) as RCOUNT from(%s)t1";
    /**
     * 查询sqlBuilder 构造器
     */
    private BaseSqlBuilder searchSqlBuilder;

    public CountPageSqlBuilder(BaseSqlBuilder searchSqlBuilder){
        this.searchSqlBuilder = searchSqlBuilder;
    }

    @Override
    public SqlExpression build(BuilderContext context) {
        SqlExpression sqlExpression = searchSqlBuilder.build(context);

        String sql = String.format(COUNT_SQL,sqlExpression.getInnerSql());

        sqlExpression.setInnerSql(sql);
        sqlExpression.setOrderBySql("");
        return sqlExpression;
    }
}
